// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   ResettableIteratorImpl.java

package com.hd123.rumba.util;

import java.io.*;
import java.util.*;

// Referenced classes of package com.intershop.beehive.foundation.util:
//            ResettableIterator, Iterators

public class ResettableIteratorImpl
    implements ResettableIterator, Serializable
{
  private static final long serialVersionUID= 300100L;
    private static class EmptyEnumeration
        implements Enumeration
    {

        public boolean hasMoreElements()
        {
            return false;
        }

        public Object nextElement()
        {
            throw new NoSuchElementException();
        }

        EmptyEnumeration()
        {
          super();
        }
    }


    private static ResettableIteratorImpl emptyIterator = null;
    private static EmptyEnumeration emptyEnumeration = null;
    private Vector buffer;
    private int pos;
    private transient Iterator source;

    public boolean hasNext()
    {
        if(buffer != null)
            return source != null && source.hasNext() || pos < buffer.size();
        else
            return false;
    }

    public Object next()
        throws NoSuchElementException
    {
        if(buffer == null)
            throw new NoSuchElementException();
        Object obj = null;
        synchronized(buffer)
        {
            if(pos < buffer.size())
            {
                obj = buffer.elementAt(pos);
            } else
            {
                if(source == null)
                    throw new NoSuchElementException();
                obj = source.next();
                buffer.addElement(obj);
            }
            pos++;
        }
        return obj;
    }

    public void reset()
    {
        pos = 0;
    }

    public final ResettableIterator cloneIterator()
    {
        ResettableIteratorImpl resettableiteratorimpl = createInstance();
        initIterator(resettableiteratorimpl);
        return resettableiteratorimpl;
    }

    protected ResettableIteratorImpl createInstance()
    {
        return new ResettableIteratorImpl();
    }

    protected void initIterator(ResettableIteratorImpl resettableiteratorimpl)
    {
        resettableiteratorimpl.buffer = buffer;
        resettableiteratorimpl.source = source;
        resettableiteratorimpl.pos = 0;
    }

    public static ResettableIterator getEmptyIterator()
    {
        if(emptyIterator == null)
            emptyIterator = new ResettableIteratorImpl();
        return emptyIterator;
    }

    public static Enumeration getEmptyEnumeration()
    {
        if(emptyEnumeration == null)
            emptyEnumeration = new EmptyEnumeration();
        return emptyEnumeration;
    }

    public Iterator toSequence()
    {
        return source;
    }

    public void remove()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException();
    }

    private void writeObject(ObjectOutputStream objectoutputstream)
        throws IOException
    {
        if(source != null)
            for(; hasNext(); next());
        objectoutputstream.defaultWriteObject();
    }

    private void readObject(ObjectInputStream objectinputstream)
        throws IOException, ClassNotFoundException
    {
        objectinputstream.defaultReadObject();
        reset();
    }

    public ResettableIteratorImpl(Iterator iterator)
    {
        if(iterator != null)
        {
            buffer = new Vector();
            source = iterator;
        }
        pos = 0;
    }

    public ResettableIteratorImpl(Enumeration enumeration)
    {
        this(Iterators.createIterator(enumeration));
    }

    protected ResettableIteratorImpl()
    {
        buffer = null;
        source = null;
        pos = 0;
    }

}
